package com.whfc.ms.api.mach.service.impl;

import com.whfc.fuum.service.SysMachTypeService;
import com.whfc.mach.dto.AppMachDTO;
import com.whfc.mach.dto.AppMachGpsDTO;
import com.whfc.mach.dto.Gps;
import com.whfc.mach.dto.obd.*;
import com.whfc.mach.enums.NetState;
import com.whfc.mach.enums.WorkState;
import com.whfc.mach.service.AppMachDataService;
import com.whfc.mach.service.ObdDataService;
import com.whfc.ms.api.mach.service.MsObdDataService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author sun_guodong
 * @description
 * @date 2020-12-22
 */
@Service
public class MsObdDataServiceImpl implements MsObdDataService {

    @DubboReference(interfaceClass = SysMachTypeService.class, version = "1.0.0")
    private SysMachTypeService sysMachTypeService;

    @DubboReference(interfaceClass = AppMachDataService.class, version = "1.0.0")
    private AppMachDataService appMachDataService;

    @DubboReference(interfaceClass = ObdDataService.class, version = "1.0.0")
    private ObdDataService ObdDataService;

    @Override
    public AppMachGpsDTO gpsLog(Integer machId, Date date) {
        AppMachGpsDTO data = new AppMachGpsDTO();
        AppMachDTO machInfo = appMachDataService.getMachInfo(machId);
        if (machInfo == null) {
            return data;
        }
        BeanUtils.copyProperties(machInfo, data);
        List<Gps> gps = ObdDataService.gpsLog(machId, date);
        data.setGpsList(gps);
        String icon = sysMachTypeService.getIcon(data.getMachType(), NetState.ONLINE.getValue(), WorkState.RUN.getValue());
        data.setIcon(icon);
        return data;
    }

    @Override
    public ObdParamDTO paramList(Integer machId) {
        ObdParamDTO data = ObdDataService.getParamList(machId);
        return data;
    }

    @Override
    public ObdParamDTO param(Integer machId) {
        ObdParamDTO data = ObdDataService.getParam(machId);
        String icon = sysMachTypeService.getIcon(data.getMachType(), data.getNetState(), data.getWorkState());
        data.setIcon(icon);
        return data;
    }

    @Override
    public ObdOilDataDTO oilData(Integer machId, Date startDate, Date endDate) {
        ObdOilDataDTO data = ObdDataService.oilData(machId, startDate, endDate);
        return data;
    }

    @Override
    public ObdOilDataDTO oilData(Integer machId, Date date) {
        ObdOilDataDTO data = ObdDataService.oilData(machId, date);
        return data;
    }

    @Override
    public ObdMileDataDTO mileData(Integer machId, Date startDate, Date endDate) {
        ObdMileDataDTO data = ObdDataService.mileData(machId, startDate, endDate);
        return data;
    }

    @Override
    public ObdMileDataDTO mileData(Integer machId, Date date) {
        ObdMileDataDTO data = ObdDataService.mileData(machId, date);
        return data;
    }

    @Override
    public ObdDataDTO getObdData(Integer machId) {
        return ObdDataService.getObdData(machId);
    }

    @Override
    public List<ObdQzDataDTO> qzData(Integer machId, Date startTime, Date endTime) {
        List<AppDeviceObdLogDTO> list = ObdDataService.qzData(machId, startTime, endTime);
        List<ObdQzDataDTO> result = new ArrayList<>(list.size());
        for (AppDeviceObdLogDTO deviceObdLogDTO : list) {
            Double force = deviceObdLogDTO.getForce();
            Double speed1 = deviceObdLogDTO.getSpeed1();
            Double speed2 = deviceObdLogDTO.getSpeed2();
            Double tension1 = deviceObdLogDTO.getTension1();
            Double tension2 = deviceObdLogDTO.getTension2();
            if (force != null || speed1 != null || speed2 != null || tension1 != null || tension2 != null) {
                ObdQzDataDTO data = new ObdQzDataDTO();
                data.setTime(deviceObdLogDTO.getTime());
                data.setForce1(force);
                data.setSpeed1(speed1);
                data.setSpeed2(speed2);
                data.setTension1(tension1);
                data.setTension2(tension2);
                result.add(data);
            }
        }
        this.handleQzData(result);
        return result;
    }

    /**
     * 处理牵张数据
     *
     * @param list
     */
    private void handleQzData(List<ObdQzDataDTO> list) {
        int size = list.size();
        for (int i = 0; i <= size - 1; i++) {
            ObdQzDataDTO curDTO = list.get(i);
            Double speed1 = curDTO.getSpeed1();
            Double speed2 = curDTO.getSpeed2();
            Double tension1 = curDTO.getTension1();
            Double tension2 = curDTO.getTension2();
            Double force1 = curDTO.getForce1();

            // 第一个数据
            if (i == 0) {
                if (speed1 == null) {
                    speed1 = 0D;
                    curDTO.setSpeed1(speed1);
                }
                if (speed2 == null) {
                    speed2 = 0D;
                    curDTO.setSpeed2(speed2);
                }
                if (tension1 == null) {
                    tension1 = 0D;
                    curDTO.setTension1(tension1);
                }
                if (tension2 == null) {
                    tension2 = 0D;
                    curDTO.setTension2(tension2);
                }
                if (force1 == null) {
                    force1 = 0D;
                    curDTO.setForce1(force1);
                }
                continue;
            }

            // 第二个数据开始
            ObdQzDataDTO preDTO = list.get(i - 1);
            if (speed1 == null) {
                curDTO.setSpeed1(preDTO.getSpeed1());
            }
            if (speed2 == null) {
                curDTO.setSpeed2(preDTO.getSpeed2());
            }
            if (tension1 == null) {
                curDTO.setTension1(preDTO.getTension1());
            }
            if (tension2 == null) {
                curDTO.setTension2(preDTO.getTension2());
            }
            if (force1 == null) {
                curDTO.setForce1(preDTO.getForce1());
            }
        }

    }


}
